
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<linux/input.h>
#include <poll.h>

/*
 * ./ts_test /dev/input/event1
 */



//poll方式
int main(int argc, char** argv)
{
    int fd;
	ssize_t readlen;
	int len = sizeof(struct input_event);
	struct input_event ev;
	struct pollfd fds[1];
	
    if(argc != 2)
	{
	    printf("Usage:%s <dev>\n",argv[0]);
		return -1;
	}
	
    //打开设备
    fd = open(argv[1], O_NONBLOCK|O_RDWR);//非阻塞方式：CPU利用率高
    //fd = open(argv[1], O_RDWR);  //阻塞方式
	if(fd == -1)
	{
	    printf("%s open error\n",argv[1]);
		return -1;
	}

	fds[0].fd = fd;
	fds[0].events = POLLIN;
	
    //读取设备
    while(1)
    {
       if(poll(fds, 1, 5000) == 0)  
       {
       	  printf("timeout\n");
       }
       else
       {
	  	    readlen = read(fd, &ev, len);
	  		if(len == readlen)
	  		{
	  		  printf("Get : type = 0x%3x, code = 0x%3x, value = %d \n",ev.type, ev.code, ev.value);
	  		}
       	}
   }
   return 0;
}
